# -*- coding: utf-8 -*-
"""
Tencent is pleased to support the open source community by making 蓝鲸智云PaaS平台社区版 (BlueKing PaaS Community
Edition) available.
Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""

from django.db import models
from django.utils import timezone


class Business(models.Model):
    """业务信息模型"""
    bk_biz_id = models.IntegerField(primary_key=True, verbose_name="业务ID")
    bk_biz_name = models.CharField(max_length=128, verbose_name="业务名称")
    created_at = models.DateTimeField(default=timezone.now, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "业务"
        verbose_name_plural = verbose_name
        db_table = "cmdb_business"

    def __str__(self):
        return f"{self.bk_biz_name}({self.bk_biz_id})"


class Set(models.Model):
    """集群信息模型"""
    bk_set_id = models.IntegerField(primary_key=True, verbose_name="集群ID")
    bk_set_name = models.CharField(max_length=128, verbose_name="集群名称")
    bk_biz_id = models.ForeignKey(Business, on_delete=models.CASCADE, verbose_name="所属业务")
    created_at = models.DateTimeField(default=timezone.now, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "集群"
        verbose_name_plural = verbose_name
        db_table = "cmdb_set"

    def __str__(self):
        return f"{self.bk_set_name}({self.bk_set_id})"


class Module(models.Model):
    """模块信息模型"""
    bk_module_id = models.IntegerField(primary_key=True, verbose_name="模块ID")
    bk_module_name = models.CharField(max_length=128, verbose_name="模块名称")
    bk_set_id = models.ForeignKey(Set, on_delete=models.CASCADE, verbose_name="所属集群")
    bk_biz_id = models.ForeignKey(Business, on_delete=models.CASCADE, verbose_name="所属业务")
    created_at = models.DateTimeField(default=timezone.now, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "模块"
        verbose_name_plural = verbose_name
        db_table = "cmdb_module"

    def __str__(self):
        return f"{self.bk_module_name}({self.bk_module_id})"


class Host(models.Model):
    """主机信息模型"""
    bk_host_id = models.IntegerField(primary_key=True, verbose_name="主机ID")
    bk_cloud_id = models.IntegerField(verbose_name="云区域ID")
    bk_host_innerip = models.GenericIPAddressField(verbose_name="主机内网IP")
    bk_os_type = models.CharField(max_length=32, verbose_name="操作系统类型")
    bk_mac = models.CharField(max_length=32, verbose_name="主机MAC地址")
    operator = models.CharField(max_length=128, verbose_name="操作人")
    bk_bak_operator = models.CharField(max_length=128, verbose_name="备份维护人")
    bk_biz_id = models.ForeignKey(Business, on_delete=models.CASCADE, verbose_name="所属业务")
    bk_set_id = models.ForeignKey(Set, null=True, blank=True, on_delete=models.SET_NULL, verbose_name="所属集群")
    bk_module_id = models.ForeignKey(Module, null=True, blank=True, on_delete=models.SET_NULL, verbose_name="所属模块")
    created_at = models.DateTimeField(default=timezone.now, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "主机"
        verbose_name_plural = verbose_name
        db_table = "cmdb_host"

    def __str__(self):
        return f"{self.bk_host_innerip}({self.bk_host_id})"


class HostDetail(models.Model):
    """主机详细信息模型"""
    host = models.OneToOneField(Host, on_delete=models.CASCADE, verbose_name="关联主机")
    cpu = models.CharField(max_length=128, verbose_name="CPU信息")
    memory = models.CharField(max_length=128, verbose_name="内存信息")
    disk = models.CharField(max_length=128, verbose_name="磁盘信息")
    os_version = models.CharField(max_length=128, verbose_name="操作系统版本")
    created_at = models.DateTimeField(default=timezone.now, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "主机详情"
        verbose_name_plural = verbose_name
        db_table = "cmdb_host_detail"

    def __str__(self):
        return f"{self.host.bk_host_innerip}的详细信息"



from django.db import models

class BackupRecord(models.Model):
    bk_host_id = models.IntegerField(verbose_name="主机ID")
    bk_file_dir = models.CharField(verbose_name="备份目录", max_length=1024)
    bk_file_suffix = models.CharField(verbose_name="文件名后缀", max_length=255)
    bk_backup_name = models.CharField(verbose_name="备份文件名", max_length=1024)
    bk_file_create_time = models.CharField(verbose_name="备份时间", max_length=30)
    bk_file_operator = models.CharField(verbose_name="备份人", max_length=30)
    bk_job_link = models.CharField(verbose_name="JOB结果", max_length=100)

    class Meta:
        verbose_name = "备份记录"
        verbose_name_plural = verbose_name


class ApiRequestCount(models.Model):
    """
    API请求次数记录模型，用于运营分析
    """

    api_category = models.CharField(verbose_name="API类别", max_length=255)
    api_name = models.CharField(verbose_name="API名称", max_length=255)
    request_count = models.IntegerField(verbose_name="请求次数", default=0)

    class Meta:
        unique_together = ("api_category", "api_name")  # 联合唯一索引
        verbose_name = "API请求次数"
        verbose_name_plural = "API请求次数"

    def __str__(self):
        return f"{self.api_category}-{self.api_name}"